import { requestApi } from '../../api';
const app = getApp();

Page({
    data: {
        lists: [],
        gid: '',
        authid: '',
        channel: '',
        start: 0,
        count: 30,
        isMoreEnd: false, // 加载更多
    },
    onLoad: function (options) {
        const { start } = this.data;
        this.setData({
            gid: options.gid || '',
        })

        this.getGoodsList(start);
    },
    onPullDownRefresh: function () {
        wx.showNavigationBarLoading(); 
        this.getGoodsList(0, true);
    },
    onReachBottom: function () {
        const { lists, isMoreEnd } = this.data;
        
        if (!isMoreEnd) {
            this.getGoodsList(lists.length);
        }
    },
    getGoodsList(start, isPullDown) {
        let { gid, count, lists, isMoreEnd } = this.data;

        if (!isPullDown) {
            wx.showLoading({
                title: '加载中...',
            });
        }

        app.request({
            url: requestApi.recomList,
            data: {
                start,
                count,
                gid,
                is_new_ver: true
            }
        }, (d) => {
            if (d.original.ret_code == 200) {

                if (start == 0) {
                    lists = d.original.goods_list
                } else {
                    lists = lists.concat(d.original.goods_list)
                }

                if (lists.length >= d.original.total) {
                    isMoreEnd = !isMoreEnd
                }

                this.setData({
                    lists,
                    isMoreEnd
                });
            }
            
        }, (error) => {
            wx.showModal({
                title: '提示',
                showCancel: false,
                content: error,
            })
        }, () => {
            wx.hideLoading();
            wx.hideNavigationBarLoading(); //完成停止加载图标
            wx.stopPullDownRefresh();
        })
    }
})